#main

// an interface with some default implementations
internal IFib {
	public TITLE = 'Fibonacci sequence'

	protected previous = 0
	protected current = 1

	protected current_index = 0
	protected max UInt

	public construct(max UInt) {
		this.max = max
	}

	public has_next() Bool {
		return this.current_index <= this.max
	}

	public get_next() UInt  // without default implementation
}

// the implementation
internal Fib: IFib {
	public get_next() UInt {
		if this.current_index > this.max {
			throw Exception("Out of range")
		}

		var temp = this.current
		this.current += this.previous
		this.previous = temp
		this.current_index += 1

		return temp
	}
}

var fib = Fib(9)
while fib.has_next() {
	echo fib.get_next()
}
catch ex {
	echo ex.getMessage()
}

// end
